<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
	
class User extends CI_Controller {

    public function User() {
        parent::__construct();
		if(!$this->session->userdata('islogin')) redirect('auth');
        $this->load->model('user_model');
		$this->load->model('log_model');
    }
	
	public function index(){
		//echo $this->session->userdata('id');
		$data['title'] = 'Manajemen User';
		$data['user'] = $this->user_model->getSingleUser($this->session->userdata('id'));
		$this->load->view('user_view', $data);
	}
	
	public function edit_nama(){
		$id = $this->input->post('id');
		$data['nama_lengkap'] = $this->input->post('nama-lengkap');
		$this->user_model->gantiNama($id, $data);
		$this->session->set_userdata($data);
		$this->session->set_flashdata('message', 'Nama lengkap anda berhasil diubah menjadi <strong>'.$data['nama_lengkap'].'</strong>!');
		
			$logdata['id_user'] = $this->session->userdata('id');
			$logdata['id_entitas'] = $id;
			$logdata['nama_tabel'] = 'user';
			$logdata['action'] = 'edit-nama';
			$this->log_model->setLogData($logdata);
		
		redirect('user');
	}
	
	public function edit_password(){
		$data['title'] = 'Ubah Password';
		$data['id'] = $this->session->userdata('id');
		$this->load->view('edit_password_view', $data);
		
		
	}
	
	public function manajemen(){
		$data['title'] = 'Manajemen User';
		
        $this->load->library('pagination');
        $query = $this->user_model->getAllUser();

        $config['base_url'] = site_url('user/manajemen/');
        $config['total_rows'] = $query->num_rows();
        $config['per_page'] = '5';
        $config['first_page'] = 'Awal'; 
		$config['last_page'] = 'Akhir'; 
		$config['next_page'] = '&laquo;'; 
		$config['prev_page'] = '&raquo;';
		$config['uri_segment'] = 3;
		//styling pagination
		$config['full_tag_open'] = '<ul class="pagination">';
		$config['full_tag_close'] = '</ul>';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		

		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        //initiation
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        if ($query->num_rows() > 0){
            //$data['hasil'] = $query;
			$data['query'] = $this->user_model->getAllUser($page, $config['per_page']);
        } else {
            $data['message'] = 'Tidak ada data tersimpan pada kategori ini.';
        }	
		
		$this->load->view('all_user_view', $data);
	}
	
	public function tambah_user(){
		//echo $this->session->userdata('id');
		$data['title'] = 'Tambah User';
		$data['user'] = $this->user_model->getSingleUser($this->session->userdata('id'));
		$this->load->view('tambah_user_view', $data);
	}
	
	public function addUser(){
	$id = $this->input->post('id');
		$data['nama_user'] = $this->input->post('username');
		$data['nama_lengkap'] = $this->input->post('nama-lengkap');
		$data['password_user'] = md5($this->input->post('password')) ;
		$data['role'] = $this->input->post('role');
		//var_dump($data);
		$this->user_model->addUser($data);
		$this->session->set_flashdata('message', 'Akun <strong>'.$data['nama_user'].'</strong> berhasil ditambahkan!');
		
		$logdata['id_user'] = $this->session->userdata('id');
		$logdata['id_entitas'] = $id;
		$logdata['nama_tabel'] = 'user';
		$logdata['action'] = 'tambah user';
		$this->log_model->setLogData($logdata);
			
		redirect('user');
	}
	
	public function changePassword(){
		$id = $this->input->post('id');
		$data['pwd_lama'] = $this->input->post('pwd-lama');
		$data['pwd_baru'] = $this->input->post('pwd-baru');
		$status = $this->user_model->gantiPassword($id, $data);
		if($status){
			$this->session->set_flashdata('message', 'Password anda berhasil diganti!');
			$logdata['id_user'] = $this->session->userdata('id');
			$logdata['id_entitas'] = $id;
			$logdata['nama_tabel'] = 'user';
			$logdata['action'] = 'edit-password';
			$this->log_model->setLogData($logdata);
			redirect('user');
		} else {
			$data['message'] = 'Password lama yang anda masukan salah!';
			$data['title'] = 'Ubah Password';
			$data['id'] = $this->session->userdata('id');
			$this->load->view('edit_password_view', $data);
		}
	}

	public function delete_user(){
		$role = $this->session->userdata('role');
		$id = $this->input->post('id');
		if($role==1 && $id != $this->session->userdata('id')){
			$this->user_model->delete_user($id);
			$logdata['id_user'] = $this->session->userdata('id');
		$logdata['id_entitas'] = $id;
		$logdata['nama_tabel'] = 'user';
		$logdata['action'] = 'hapus user';
		$this->log_model->setLogData($logdata);
		redirect('user');
		} else {
			$this->session->set_flashdata('message', 'Anda tidak memiliki kewenangan untuk mengakses halaman ini!');
			redirect('error');
		}
	}

	public function edit_role($id){
		$role = $this->session->userdata('role');
		if($role==1 &&  $id != $this->session->userdata('id')){
			$data['hasil'] = $this->user_model->getSingleUser($id);
			$data['title'] = 'Edit Role';
			$this->load->view('edit_role_view', $data);
		} else {
			$this->session->set_flashdata('message', 'Anda tidak memiliki kewenangan untuk mengakses halaman ini!');
			redirect('error');
		}
	}

	public function ubah_role(){
		$role = $this->session->userdata('role');

		$id = $this->input->post('id');
		$role_user = $this->input->post('role');
		if($role==1 &&  $id != $this->session->userdata('id')){
			$this->user_model->ubah_role($id, $role_user);
			$logdata['id_user'] = $this->session->userdata('id');
		$logdata['id_entitas'] = $id;
		$logdata['nama_tabel'] = 'user';
		$logdata['action'] = 'ubah role';
		$this->log_model->setLogData($logdata);
			redirect('user/manajemen');
		} else {
			$this->session->set_flashdata('message', 'Anda tidak memiliki kewenangan untuk mengakses halaman ini!');
			redirect('error');
		}
	}
}